﻿<cfscript>
/** 
* @hint "acadmicBase - Course"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.courseListener" 
	output=false
	depends="sequenceCourse,sessionAdvice,datetimeAdvice" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	/**
	* @hint "删除课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteCourse( required MachII.framework.Event event ) output=false {
		
		var course = "";
		var passArg = structNew();
		var sql = "";
		

		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "SELECT a.institute_id,a.cat_id 
					FROM t_course a 
					WHERE a.cid = :cId ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="cId", value= event.getArg("CourseID"), cfsqltype="cf_sql_varchar" );
			var rs_course = queryObj.execute( sql=sql ).getResult();
			
			sql = "DELETE FROM t_course 
							WHERE cid = :cId ";
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="cId", value=event.getArg("CourseID"), cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );
			
			
			event.setArg("DepID", rs_course.institute_id);
			event.setArg("Cat", rs_course.cat_id);
			
		}
				
	}
	
	/**
	* @hint "更新课程附加信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateTaskCourseInfo( required MachII.framework.Event event ) output=false {
		
		var course = "";
		var passArg = structNew();
		
		var taskId = event.getArg("TSK");
		
		var courseEng = left( event.getArg("CourseEnglish"), 90 );
		var courseIntro = left( event.getArg("Intro"), 2000 );
		var courseRefBooks = left( event.getArg("ReferenceBooks"), 1000 );
		
		var courseId = event.getArg("CourseID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "UPDATE 
						t_course 
					SET 
						course_english = :courseEng, 
						intro = :courseIntro ,
						reference_books = :courseRefBooks  
					WHERE 
						cid = :courseId ";
			queryObj = new query( datasource=application.dnsMaster);
			queryObj.addParam( name="courseEng", value=courseEng, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseIntro", value=courseIntro, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseRefBooks", value=courseRefBooks, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );
				
			event.setArg("TSK", taskId);

		}
		
	}
	
	/**
	* @hint "更新课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateCourse( required MachII.framework.Event event ) output=false {
			
		var tchId = getsessionAdvice().getAutherUserID();

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "UPDATE 
						t_course a 
					SET 
						a.institute_id = :instituteId,
						a.course_name = :courseName, 
						a.course_english = :engLish, 
						a.course_credit = :credit, 
						a.period_week = :periodWeek, 
						a.period = :period, 
						a.period_theory = :periodTheory, 
						a.period_practice = :periodPractice,
						a.period_computer = :periodComputer, 
						a.intro = :intro, 
						a.reference_books = :referenceBooks, 
						a.cat_id = :catId, 
						a.course_code = :courseCode, 
						a.mark_mode = :markMode, 
						a.resit_policy = :resitPolicy, 
						a.abandon = :abandon,
						a.course_type = :courseType,
						a.last_modified = :lastModified, 
						a.last_mender = :lastMender,
						a.prop_id = :propId  
					WHERE 
						a.cid = :cid ";
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="instituteId", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseName", value=event.getArg("CourseName"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="engLish", value=event.getArg("CourseEnglish"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="credit", value=event.getArg("Credit"), cfsqltype="cf_sql_decimal", scale=2 );
			queryObj.addParam( name="periodWeek", value=event.getArg("PeriodPerWeek"), cfsqltype="cf_sql_decimal", scale=1 );
			queryObj.addParam( name="period", value=event.getArg("PeriodTheory")+event.getArg("PeriodPractice")+event.getArg("PeriodComputer"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="periodTheory", value=event.getArg("PeriodTheory"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="periodPractice", value=event.getArg("PeriodPractice"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="periodComputer", value=event.getArg("PeriodComputer"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="intro", value=event.getArg("Intro"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="referenceBooks", value=event.getArg("ReferenceBooks"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="catId", value=event.getArg("CatID"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="courseCode", value=event.getArg("CourseCode"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="markMode", value=event.getArg("MarkMode", '0'), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="resitPolicy", value=event.getArg("ResitPolicy", '1'), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="abandon", value=event.getArg("Abandon", '0'), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cid", value=event.getArg("CourseID"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="lastModified", value=getdatetimeAdvice().getCurrentDatetimeString() , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="lastMender", value=tchId , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="propId", value=event.getArg("courseProp"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseType", value=event.getArg("courseProp")-1, cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );
						
			event.setArg("DepID", event.getArg("InstituteID"));
			event.setArg("Cat", event.getArg("CatID"));
			event.setArg("CID", event.getArg("CourseID"));
			
		}
	}
	
	/**
	* @hint "创建课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createCourse( required MachII.framework.Event event ) output=false {
	
		var passArg = structNew();
		var sequenceCourse = getsequenceCourse();
		var courseId = sequenceCourse.getID();
		
		var tchId = getsessionAdvice().getAutherUserID();
		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* 自动生成课程号 */
			var sql = "INSERT INTO t_course (
							    course_name,
								course_credit,
								period_theory,
								period_practice,
								period_computer,
								period_week,
								course_english,
								intro,
								reference_books,
								course_code,
								mark_mode,
								resit_policy,
								abandon,
								period,
								institute_id,
								cat_id,
								cid,
								course_id,
								last_modified,
								last_mender, 
								course_type,
								t_id,
								prop_id  
						)
						 VALUES(
								:CourseName ,
								:Credit ,
								:PeriodTheory ,
								:PeriodPractice ,
								:PeriodComputer ,
								:PeriodPerWeek ,
								:CourseEnglish ,
								:Intro ,
								:ReferenceBooks ,
								:CourseCode ,
								:MarkMode ,
								:ResitPolicy ,
								:Abandon ,
								:Period ,
								:Institute ,
								:Catalog ,
								:Cid, 
								:Cid,
								:lastModified, 
								:lastMender,
								:courseType, 
								:tchId,
								:propId  
						 )";
			var queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="CourseName", value=event.getArg("CourseName") , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Credit", value=event.getArg("Credit"), cfsqltype="cf_sql_decimal", scale = 2 );
			queryObj.addParam( name="PeriodTheory", value=event.getArg("PeriodTheory"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="PeriodPractice", value=event.getArg("PeriodPractice"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="PeriodComputer", value=event.getArg("PeriodComputer") , cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="PeriodPerWeek", value=event.getArg("PeriodPerWeek"), cfsqltype="cf_sql_decimal", scale = 1 );
			queryObj.addParam( name="CourseEnglish", value= event.getArg("CourseEnglish"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Intro", value=event.getArg("Intro"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="ReferenceBooks", value=event.getArg("ReferenceBooks"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="CourseCode", value=event.getArg("CourseCode"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="MarkMode", value=event.getArg("MarkMode", '0') , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ResitPolicy", value= event.getArg("ResitPolicy", '1') , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="Abandon", value=event.getArg("Abandon", '0'), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Period", value= event.getArg("PeriodTheory") +  event.getArg("PeriodPractice") + event.getArg("PeriodComputer"), cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="Institute", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Catalog", value=event.getArg("CatID"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="Cid", value=courseId , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="lastModified", value=getdatetimeAdvice().getCurrentDatetimeString() , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="lastMender", value=tchId , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tchId", value='0' , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="propId", value=event.getArg("courseProp") , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseType", value=event.getArg("courseProp")-1 , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			structInsert(passArg, "DepID", event.getArg("InstituteID"));
			structInsert(passArg, "Cat", event.getArg("CatID"));
			structInsert(passArg, "CID", courseId);
			structInsert(passArg, "PeriodPerWeek", event.getArg("PeriodPerWeek"));
		

			event.setArgs(passArg);
					
		}		
	}
	
}

</cfscript>
